package com.king.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.king.model.common.RestResponse;
import com.king.model.po.User;
import com.king.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author king
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public RestResponse<Boolean>  login( User user){
        if (StringUtils.isBlank(user.getName())){
            return RestResponse.fail("请输入账号名");
        }

        if (StringUtils.isBlank(user.getPassword())){
            return RestResponse.fail("请输入密码");
        }
        User one = userService.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getName, user.getName())
                .eq(User::getPassword, user.getPassword())
        );
        if (one==null){
            return RestResponse.fail("请输入正确得用户名和密码");
        }
        if (!one.getRole()){
            return RestResponse.fail("你没有权限登录");
        }

        return RestResponse.success(true);
    }
}
